﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework;
using TrueSync;
using TrueSync.Physics2D;

public class RectsGizmos : MonoBehaviour
{
    ObjHandle<LogicEntity> lEntity;


    private void Start()
    {
        lEntity = GameManager.Instance.GetEntity(GetComponent<VisualEntity>().entityID);
    }

    private void OnDrawGizmos()
    {
        if (lEntity.IsValid == false)
            return;

        LogicPhysicsComponent physicsComponent = lEntity.handle.GetLogicComponent<LogicPhysicsComponent>();
        Fixture bodyPositionRect = physicsComponent.BodyPositionRect;
        LogicMoveComponent moveComponent = lEntity.handle.GetLogicComponent<LogicMoveComponent>();
        TSVector2 max = ((RectangleShape)bodyPositionRect.Shape).max, min = ((RectangleShape)bodyPositionRect.Shape).min;
        max = moveComponent.Transform.TransformPoint(max); min = moveComponent.Transform.TransformPoint(min);
        DrawBoxGizmo(max, min);
    }

    void DrawBoxGizmo(TSVector2 max, TSVector2 min)
    {
        Gizmos.DrawLine(new Vector3(max.x.AsFloat(), max.y.AsFloat(), 0), new Vector3(max.x.AsFloat(), min.y.AsFloat(), 0));
        Gizmos.DrawLine(new Vector3(max.x.AsFloat(), min.y.AsFloat(), 0), new Vector3(min.x.AsFloat(), min.y.AsFloat(), 0));
        Gizmos.DrawLine(new Vector3(min.x.AsFloat(), min.y.AsFloat(), 0), new Vector3(min.x.AsFloat(), max.y.AsFloat(), 0));
        Gizmos.DrawLine(new Vector3(min.x.AsFloat(), max.y.AsFloat(), 0), new Vector3(max.x.AsFloat(), max.y.AsFloat(), 0));
    }

}
